package com.lebang.controller.api.earned.book;

import com.lebang.aop.Sessionid;
import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.BookChapterFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 听书章节
 *
 * @author ljx
 * @date 2020-11-02
 */
@Api(tags = "听书章节")
@RestController
@RequestMapping("/book/chapter")
public class BookChapterApi {
    @Autowired
    private BookChapterFeign bookChapterFeign;
    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNoticeService memberNoticeService;


    /**
     * 获取听书时长阶段配置信息
     *
     * @param request
     * @return
     */
    @Require
    @ApiOperation("获取听书章节阶段配置信息")
    @GetMapping("/configList")
    public JsonData configList(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return bookChapterFeign.configList(userInfo);
    }

    /**
     * 听书章节配置记录添加
     *
     * @param request
     * @param configId
     * @return
     */
    @Require
    @ApiOperation("听书章节记录添加")
    @PostMapping("/chapterConfig/addDataRecord")
    @MemberLock
    public JsonData bookTimeData(HttpServletRequest request,
                                 @ApiParam(name = "配置id") @RequestParam("configId") Integer configId,
                                 @ApiParam(name = "听书id") @RequestParam("bookId") String bookId,
                                 @ApiParam(name = "章节") @RequestParam("chapter") String chapter) {
        RequestUserInfo userInfo = new RequestUserInfo(request);

        return bookChapterFeign.bookTimeData(userInfo, configId, bookId, chapter);
    }

    /**
     * 获取所有听书累计收益
     *
     * @param request
     * @return
     */
    @Require
    @ApiOperation("获取所有听书累计收益")
    @PostMapping("/income")
    public JsonData bookIncome(HttpServletRequest request) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        return bookChapterFeign.bookIncome(userInfo);
    }

    /**
     * 领取听书累计收益
     *
     * @param request
     * @param bookId
     * @return
     */
    @Require
    @Sessionid
    @ApiOperation("领取听书累计收益")
    @PostMapping("/pull/income")
    @MemberLock
    public JsonData pullBookIncome(HttpServletRequest request, @ApiParam(name = "听书id") @RequestParam("bookId") String bookId) {
        RequestHeaders headers = new RequestHeaders(request);
        RequestUserInfo userInfo = new RequestUserInfo(request);

        if (TextUtil.isEmpty(bookId)) {
            return Callback.fail("fail", "领取失败");
        }
        //累计听书金币
        Result<Integer> result = bookChapterFeign.pullIncome(userInfo, bookId);
        if (!result.isSuccess()) {
            return Callback.fail("fail", "领取失败");
        } else {
            pointService.addPoint(headers.getUserid(), result.getData(),
                    TaskCategory.BOOK_CHAPTER, headers, "完成听书累计收益奖励" + result.getData() + "金币");
            memberNoticeService.addNoticeAndPush("听书赚", "完成听书累计收益奖励" + result.getData() + "金币", headers.getUserid(), headers);

            return Callback.success("success", "领取成功", result.getData());
        }
    }
}


